package com.tz.actions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tz.entity.Cart;
import com.tz.entity.Commodity;
import com.tz.entity.Product;
import com.tz.service.ProductService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Tinyang on 2016/7/24.
 */
@Namespace("/product")
@ParentPackage("json-default")
public class ShoppingCartAction extends ActionSupport {
    private long product_id;
    private int number;
    private Cart cart;
    @Autowired
    private ProductService productService;

    @Action(value = "shoppingCart", results = {
            @Result(name = SUCCESS, type = "json", params = {"root", "cart"})
    })
    public String Session() {
        Commodity commodity;
        Product product = productService.getProduct(product_id);
        List<Commodity> commodities = new ArrayList<Commodity>();
        Map<String, Object> session = ActionContext.getContext().getSession();
        if (session.get("cart") == null) {
            cart = new Cart();
            commodity = new Commodity();
            commodity.setNumber(number);
            commodity.setProduct(product);
            commodities.add(commodity);
            cart.setTotalPrice((product.getPrice()) * number);
            cart.setCommodities(commodities);
            session.put("cart", cart);
        } else {
            cart = (Cart) session.get("cart");
            commodities = cart.getCommodities();
            double totalprice = cart.getTotalPrice();
            boolean flag = true;
            for (int i = 0; i < commodities.size(); i++) {
                if (commodities.get(i).getProduct().getId() == product_id) {
                    number = (commodities.get(i).getNumber() + number);
                    totalprice = commodities.get(i).getProduct().getPrice() * number ;
                    commodities.get(i).setNumber(number);
                    cart.setTotalPrice(totalprice);
                    flag = false;
                } else {
                    continue;
                }
            }
                if (flag) {
                    commodity = new Commodity(product, number);
                    commodities.add(commodity);
                    totalprice = (product.getPrice() * number + totalprice);
                    cart.setTotalPrice(totalprice);
                    cart.setCommodities(commodities);
                }

            session.put("cart", cart);
        }
        return SUCCESS;
    }


    public long getProduct_id() {
        return product_id;
    }

    public void setProduct_id(long product_id) {
        this.product_id = product_id;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public Cart getCart() {
        return cart;
    }

    public void setCart(Cart cart) {
        this.cart = cart;
    }
}
